home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / bin / pildriver.py < prev    next >
Encoding:
Python Source  |  2010-07-26  |  15.0 KB  |  525 lines

  1. #! /usr/bin/python
  2. """PILdriver, an image-processing calculator using PIL.
  3.  
  4. An instance of class PILDriver is essentially a software stack machine
  5. (Polish-notation interpreter) for sequencing PIL image
  6. transformations.  The state of the instance is the interpreter stack.
  7.  
  8. The only method one will normally invoke after initialization is the
  9. `execute' method.  This takes an argument list of tokens, pushes them
  10. onto the instance's stack, and then tries to clear the stack by
  11. successive evaluation of PILdriver operators.  Any part of the stack
  12. not cleaned off persists and is part of the evaluation context for
  13. the next call of the execute method.
  14.  
  15. PILDriver doesn't catch any exceptions, on the theory that these
  16. are actually diagnostic information that should be interpreted by
  17. the calling code.
  18.  
  19. When called as a script, the command-line arguments are passed to
  20. a PILDriver instance.  If there are no command-line arguments, the
  21. module runs an interactive interpreter, each line of which is split into
  22. space-separated tokens and passed to the execute method.
  23.  
  24. In the method descriptions below, a first line beginning with the string
  25. `usage:' means this method can be invoked with the token that follows
  26. it.  Following <>-enclosed arguments describe how the method interprets
  27. the entries on the stack.  Each argument specification begins with a
  28. type specification: either `int', `float', `string', or `image'.
  29.  
  30. All operations consume their arguments off the stack (use `dup' to
  31. keep copies around).  Use `verbose 1' to see the stack state displayed
  32. before each operation.
  33.  
  34. Usage examples:
  35.  
  36.     `show crop 0 0 200 300 open test.png' loads test.png, crops out a portion
  37. of its upper-left-hand corner and displays the cropped portion.
  38.  
  39.     `save rotated.png rotate 30 open test.tiff' loads test.tiff, rotates it
  40. 30 degrees, and saves the result as rotated.png (in PNG format).
  41. """
  42. # by Eric S. Raymond <esr@thyrsus.com>
  43. # $Id$
  44.  
  45. # TO DO:
  46. # 1. Add PILFont capabilities, once that's documented.
  47. # 2. Add PILDraw operations.
  48. # 3. Add support for composing and decomposing multiple-image files.
  49. #
  50.  
  51. from PIL import Image
  52. import string
  53.  
  54. class PILDriver:
  55.  
  56.     verbose = 0
  57.  
  58.     def do_verbose(self):
  59.         """usage: verbose <int:num>
  60.  
  61.         Set verbosity flag from top of stack.
  62.         """
  63.         self.verbose = self.do_pop()
  64.  
  65.     # The evaluation stack (internal only)
  66.  
  67.     stack = []          # Stack of pending operations
  68.  
  69.     def push(self, item):
  70.         "Push an argument onto the evaluation stack."
  71.         self.stack = [item] + self.stack
  72.  
  73.     def top(self):
  74.         "Return the top-of-stack element."
  75.         return self.stack[0]
  76.  
  77.     # Stack manipulation (callable)
  78.  
  79.     def do_clear(self):
  80.         """usage: clear
  81.  
  82.         Clear the stack.
  83.         """
  84.         self.stack = []
  85.  
  86.     def do_pop(self):
  87.         """usage: pop
  88.  
  89.         Discard the top element on the stack.
  90.         """
  91.         top = self.stack[0]
  92.         self.stack = self.stack[1:]
  93.         return top
  94.  
  95.     def do_dup(self):
  96.         """usage: dup
  97.  
  98.         Duplicate the top-of-stack item.
  99.         """
  100.         if hasattr(self, 'format'):     # If it's an image, do a real copy
  101.             dup = self.stack[0].copy()
  102.         else:
  103.             dup = self.stack[0]
  104.         self.stack = [dup] + self.stack
  105.  
  106.     def do_swap(self):
  107.         """usage: swap
  108.  
  109.         Swap the top-of-stack item with the next one down.
  110.         """
  111.         self.stack = [self.stack[1], self.stack[0]] + self.stack[2:]
  112.  
  113.     # Image module functions (callable)
  114.  
  115.     def do_new(self):
  116.         """usage: new <int:xsize> <int:ysize> <int:color>:
  117.  
  118.         Create and push a greyscale image of given size and color.
  119.         """
  120.         xsize = int(self.do_pop())
  121.         ysize = int(self.do_pop())
  122.         color = int(self.do_pop())
  123.         self.push(Image.new("L", (xsize, ysize), color))
  124.  
  125.     def do_open(self):
  126.         """usage: open <string:filename>
  127.  
  128.         Open the indicated image, read it, push the image on the stack.
  129.         """
  130.         self.push(Image.open(self.do_pop()))
  131.  
  132.     def do_blend(self):
  133.         """usage: blend <image:pic1> <image:pic2> <float:alpha>
  134.  
  135.         Replace two images and an alpha with the blended image.
  136.         """
  137.         image1 = self.do_pop()
  138.         image2 = self.do_pop()
  139.         alpha = float(self.do_pop())
  140.         self.push(Image.blend(image1, image2, alpha))
  141.  
  142.     def do_composite(self):
  143.         """usage: composite <image:pic1> <image:pic2> <image:mask>
  144.  
  145.         Replace two images and a mask with their composite.
  146.         """
  147.         image1 = self.do_pop()
  148.         image2 = self.do_pop()
  149.         mask = self.do_pop()
  150.         self.push(Image.composite(image1, image2, mask))
  151.  
  152.     def do_merge(self):
  153.         """usage: merge <string:mode> <image:pic1> [<image:pic2> [<image:pic3> [<image:pic4>]]]
  154.  
  155.         Merge top-of stack images in a way described by the mode.
  156.         """
  157.         mode = self.do_pop()
  158.         bandlist = []
  159.         for band in mode:
  160.             bandlist.append(self.do_pop())
  161.         self.push(Image.merge(mode, bandlist))
  162.  
  163.     # Image class methods
  164.  
  165.     def do_convert(self):
  166.         """usage: convert <string:mode> <image:pic1>
  167.  
  168.         Convert the top image to the given mode.
  169.         """
  170.         mode = self.do_pop()
  171.         image = self.do_pop()
  172.         self.push(image.convert(mode))
  173.  
  174.     def do_copy(self):
  175.         """usage: copy <image:pic1>
  176.  
  177.         Make and push a true copy of the top image.
  178.         """
  179.         self.dup()
  180.  
  181.     def do_crop(self):
  182.         """usage: crop <int:left> <int:upper> <int:right> <int:lower> <image:pic1>
  183.  
  184.         Crop and push a rectangular region from the current image.
  185.         """
  186.         left = int(self.do_pop())
  187.         upper = int(self.do_pop())
  188.         right = int(self.do_pop())
  189.         lower = int(self.do_pop())
  190.         image = self.do_pop()
  191.         self.push(image.crop((left, upper, right, lower)))
  192.  
  193.     def do_draft(self):
  194.         """usage: draft <string:mode> <int:xsize> <int:ysize>
  195.  
  196.         Configure the loader for a given mode and size.
  197.         """
  198.         mode = self.do_pop()
  199.         xsize = int(self.do_pop())
  200.         ysize = int(self.do_pop())
  201.         self.push(self.draft(mode, (xsize, ysize)))
  202.  
  203.     def do_filter(self):
  204.         """usage: filter <string:filtername> <image:pic1>
  205.  
  206.         Process the top image with the given filter.
  207.         """
  208.         import ImageFilter
  209.         filter = eval("ImageFilter." + string.upper(self.do_pop()))
  210.         image = self.do_pop()
  211.         self.push(image.filter(filter))
  212.  
  213.     def do_getbbox(self):
  214.         """usage: getbbox
  215.  
  216.         Push left, upper, right, and lower pixel coordinates of the top image.
  217.         """
  218.         bounding_box = self.do_pop().getbbox()
  219.         self.push(bounding_box[3])
  220.         self.push(bounding_box[2])
  221.         self.push(bounding_box[1])
  222.         self.push(bounding_box[0])
  223.  
  224.     def do_getextrema(self):
  225.         """usage: extrema
  226.  
  227.         Push minimum and maximum pixel values of the top image.
  228.         """
  229.         extrema = self.do_pop().extrema()
  230.         self.push(extrema[1])
  231.         self.push(extrema[0])
  232.  
  233.     def do_offset(self):
  234.         """usage: offset <int:xoffset> <int:yoffset> <image:pic1>
  235.  
  236.         Offset the pixels in the top image.
  237.         """
  238.         xoff = int(self.do_pop())
  239.         yoff = int(self.do_pop())
  240.         image = self.do_pop()
  241.         self.push(image.offset(xoff, yoff))
  242.  
  243.     def do_paste(self):
  244.         """usage: paste <image:figure> <int:xoffset> <int:yoffset> <image:ground>
  245.  
  246.         Paste figure image into ground with upper left at given offsets.
  247.         """
  248.         figure = self.do_pop()
  249.         xoff = int(self.do_pop())
  250.         yoff = int(self.do_pop())
  251.         ground = self.do_pop()
  252.         if figure.mode == "RGBA":
  253.             ground.paste(figure, (xoff, yoff), figure)
  254.         else:
  255.             ground.paste(figure, (xoff, yoff))
  256.         self.push(ground)
  257.  
  258.     def do_resize(self):
  259.         """usage: resize <int:xsize> <int:ysize> <image:pic1>
  260.  
  261.         Resize the top image.
  262.         """
  263.         ysize = int(self.do_pop())
  264.         xsize = int(self.do_pop())
  265.         image = self.do_pop()
  266.         self.push(image.resize((xsize, ysize)))
  267.  
  268.     def do_rotate(self):
  269.         """usage: rotate <int:angle> <image:pic1>
  270.  
  271.         Rotate image through a given angle
  272.         """
  273.         angle = int(self.do_pop())
  274.         image = self.do_pop()
  275.         self.push(image.rotate(angle))
  276.  
  277.     def do_save(self):
  278.         """usage: save <string:filename> <image:pic1>
  279.  
  280.         Save image with default options.
  281.         """
  282.         filename = self.do_pop()
  283.         image = self.do_pop()
  284.         image.save(filename)
  285.  
  286.     def do_save2(self):
  287.         """usage: save2 <string:filename> <string:options> <image:pic1>
  288.  
  289.         Save image with specified options.
  290.         """
  291.         filename = self.do_pop()
  292.         options = self.do_pop()
  293.         image = self.do_pop()
  294.         image.save(filename, None, options)
  295.  
  296.     def do_show(self):
  297.         """usage: show <image:pic1>
  298.  
  299.         Display and pop the top image.
  300.         """
  301.         self.do_pop().show()
  302.  
  303.     def do_thumbnail(self):
  304.         """usage: thumbnail <int:xsize> <int:ysize> <image:pic1>
  305.  
  306.         Modify the top image in the stack to contain a thumbnail of itself.
  307.         """
  308.         ysize = int(self.do_pop())
  309.         xsize = int(self.do_pop())
  310.         self.top().thumbnail((xsize, ysize))
  311.  
  312.     def do_transpose(self):
  313.         """usage: transpose <string:operator> <image:pic1>
  314.  
  315.         Transpose the top image.
  316.         """
  317.         transpose = string.upper(self.do_pop())
  318.         image = self.do_pop()
  319.         self.push(image.transpose(transpose))
  320.  
  321.     # Image attributes
  322.  
  323.     def do_format(self):
  324.         """usage: format <image:pic1>
  325.  
  326.         Push the format of the top image onto the stack.
  327.         """
  328.         self.push(self.pop().format)
  329.  
  330.     def do_mode(self):
  331.         """usage: mode <image:pic1>
  332.  
  333.         Push the mode of the top image onto the stack.
  334.         """
  335.         self.push(self.pop().mode)
  336.  
  337.     def do_size(self):
  338.         """usage: size <image:pic1>
  339.  
  340.         Push the image size on the stack as (y, x).
  341.         """
  342.         size = self.pop().size
  343.         self.push(size[0])
  344.         self.push(size[1])
  345.  
  346.     # ImageChops operations
  347.  
  348.     def do_invert(self):
  349.         """usage: invert <image:pic1>
  350.  
  351.         Invert the top image.
  352.         """
  353.         import ImageChops
  354.         self.push(ImageChops.invert(self.do_pop()))
  355.  
  356.     def do_lighter(self):
  357.         """usage: lighter <image:pic1> <image:pic2>
  358.  
  359.         Pop the two top images, push an image of the lighter pixels of both.
  360.         """
  361.         import ImageChops
  362.         image1 = self.do_pop()
  363.         image2 = self.do_pop()
  364.         self.push(ImageChops.lighter(image1, image2))
  365.  
  366.     def do_darker(self):
  367.         """usage: darker <image:pic1> <image:pic2>
  368.  
  369.         Pop the two top images, push an image of the darker pixels of both.
  370.         """
  371.         import ImageChops
  372.         image1 = self.do_pop()
  373.         image2 = self.do_pop()
  374.         self.push(ImageChops.darker(image1, image2))
  375.  
  376.     def do_difference(self):
  377.         """usage: difference <image:pic1> <image:pic2>
  378.  
  379.         Pop the two top images, push the difference image
  380.         """
  381.         import ImageChops
  382.         image1 = self.do_pop()
  383.         image2 = self.do_pop()
  384.         self.push(ImageChops.difference(image1, image2))
  385.  
  386.     def do_multiply(self):
  387.         """usage: multiply <image:pic1> <image:pic2>
  388.  
  389.         Pop the two top images, push the multiplication image.
  390.         """
  391.         import ImageChops
  392.         image1 = self.do_pop()
  393.         image2 = self.do_pop()
  394.         self.push(ImageChops.multiply(image1, image2))
  395.  
  396.     def do_screen(self):
  397.         """usage: screen <image:pic1> <image:pic2>
  398.  
  399.         Pop the two top images, superimpose their inverted versions.
  400.         """
  401.         import ImageChops
  402.         image2 = self.do_pop()
  403.         image1 = self.do_pop()
  404.         self.push(ImageChops.screen(image1, image2))
  405.  
  406.     def do_add(self):
  407.         """usage: add <image:pic1> <image:pic2> <int:offset> <float:scale>
  408.  
  409.         Pop the two top images, produce the scaled sum with offset.
  410.         """
  411.         import ImageChops
  412.         image1 = self.do_pop()
  413.         image2 = self.do_pop()
  414.         scale = float(self.do_pop())
  415.         offset = int(self.do_pop())
  416.         self.push(ImageChops.add(image1, image2, scale, offset))
  417.  
  418.     def do_subtract(self):
  419.         """usage: subtract <image:pic1> <image:pic2> <int:offset> <float:scale>
  420.  
  421.         Pop the two top images, produce the scaled difference with offset.
  422.         """
  423.         import ImageChops
  424.         image1 = self.do_pop()
  425.         image2 = self.do_pop()
  426.         scale = float(self.do_pop())
  427.         offset = int(self.do_pop())
  428.         self.push(ImageChops.subtract(image1, image2, scale, offset))
  429.  
  430.     # ImageEnhance classes
  431.  
  432.     def do_color(self):
  433.         """usage: color <image:pic1>
  434.  
  435.         Enhance color in the top image.
  436.         """
  437.         import ImageEnhance
  438.         factor = float(self.do_pop())
  439.         image = self.do_pop()
  440.         enhancer = ImageEnhance.Color(image)
  441.         self.push(enhancer.enhance(factor))
  442.  
  443.     def do_contrast(self):
  444.         """usage: contrast <image:pic1>
  445.  
  446.         Enhance contrast in the top image.
  447.         """
  448.         import ImageEnhance
  449.         factor = float(self.do_pop())
  450.         image = self.do_pop()
  451.         enhancer = ImageEnhance.Color(image)
  452.         self.push(enhancer.enhance(factor))
  453.  
  454.     def do_brightness(self):
  455.         """usage: brightness <image:pic1>
  456.  
  457.         Enhance brightness in the top image.
  458.         """
  459.         import ImageEnhance
  460.         factor = float(self.do_pop())
  461.         image = self.do_pop()
  462.         enhancer = ImageEnhance.Color(image)
  463.         self.push(enhancer.enhance(factor))
  464.  
  465.     def do_sharpness(self):
  466.         """usage: sharpness <image:pic1>
  467.  
  468.         Enhance sharpness in the top image.
  469.         """
  470.         import ImageEnhance
  471.         factor = float(self.do_pop())
  472.         image = self.do_pop()
  473.         enhancer = ImageEnhance.Color(image)
  474.         self.push(enhancer.enhance(factor))
  475.  
  476.     # The interpreter loop
  477.  
  478.     def execute(self, list):
  479.         "Interpret a list of PILDriver commands."
  480.         list.reverse()
  481.         while len(list) > 0:
  482.             self.push(list[0])
  483.             list = list[1:]
  484.             if self.verbose:
  485.                 print "Stack: " + `self.stack`
  486.             top = self.top()
  487.             if type(top) != type(""):
  488.                 continue;
  489.             funcname = "do_" + top
  490.             if not hasattr(self, funcname):
  491.                 continue
  492.             else:
  493.                 self.do_pop()
  494.                 func = getattr(self, funcname)
  495.                 func()
  496.  
  497. if __name__ == '__main__':
  498.     import sys
  499.     try:
  500.         import readline
  501.     except ImportError:
  502.         pass # not available on all platforms
  503.  
  504.     # If we see command-line arguments, interpret them as a stack state
  505.     # and execute.  Otherwise go interactive.
  506.  
  507.     driver = PILDriver()
  508.     if len(sys.argv[1:]) > 0:
  509.         driver.execute(sys.argv[1:])
  510.     else:
  511.         print "PILDriver says hello."
  512.         while 1:
  513.             try:
  514.                 line = raw_input('pildriver> ');
  515.             except EOFError:
  516.                 print "\nPILDriver says goodbye."
  517.                 break
  518.             driver.execute(string.split(line))
  519.             print driver.stack
  520.  
  521. # The following sets edit modes for GNU EMACS
  522. # Local Variables:
  523. # mode:python
  524. # End:
  525.